package com.ls.fw.hadoop.client.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import org.apache.commons.lang.time.DateFormatUtils;

/**
 * 日期公用类
 * <p>
 * Title: Framework
 * </p>
 * <p>
 * Description: Framework
 * </p>
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 * <p>
 * Company: xwtech.com
 * </p>
 */
public class DateTimeUtil {

	// 常量------------------------------------------------------------------
	// 格式：2007年06月07日 12时12分12秒234毫秒
	private final static String[] FORMAT_CHINA = { "年", "月", "日", "时", "分",
			"秒", "毫秒" };
	/**
	 * 格式：2007-06-07 12:12:12 234
	 */
	private final static String[] FORMAT_NORMAL = { "-", "-", "", ":", ":", "",
			"" };
	/**
	 * 格式：2007/06/07 12:12:12 234
	 */
	private final static String[] FORMAT_DATATIME = { "/", "/", "", ":", ":",
			"", "" };
	/**
	 * 格式：中文星期
	 */
	private final static String[] FORMAT_WEEK = { "星期日", "星期一", "星期二", "星期三",
			"星期四", "星期五", "星期六" };

	/**
	 * 获取今日年份
	 * 
	 * @return yyyy
	 */
	public static String getTodayYear() {
		return DateFormatUtils.format(new Date(), "yyyy");
	}

	/**
	 * 获取今日月份
	 */
	public static String getTodayMonth() {
		return DateFormatUtils.format(new Date(), "MM");
	}

	/**
	 * 获取今日日期
	 * 
	 * @return dd
	 */
	public static String getTodayDay() {
		return DateFormatUtils.format(new Date(), "dd");
	}

	/**
	 * 获取短日月
	 * 
	 * @return MMdd
	 */
	public static String getTodayChar4() {
		return DateFormatUtils.format(new Date(), "MMdd");
	}

	/**
	 * 返回年月
	 * 
	 * @return yyyyMM
	 */
	public static String getTodayChar6() {
		return DateFormatUtils.format(new Date(), "yyyyMM");
	}

	/**
	 * 返回年月日
	 * 
	 * @return yyyyMMdd
	 */
	public static String getTodayChar8() {
		return DateFormatUtils.format(new Date(), "yyyyMMdd");
	}

	/**
	 * 返回年月日小时
	 * 
	 * @return yyyyMMdd
	 */
	public static String getTodayChar10() {
		return DateFormatUtils.format(new Date(), "yyyyMMddHH");
	}

	/**
	 * 返回 年月日小时分
	 * 
	 * @return yyyyMMddHHmm
	 */
	public static String getTodayChar12() {
		return DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
	}

	/**
	 * 返回 年月日小时分秒
	 * 
	 * @return yyyyMMddHHmmss
	 */
	public static String getTodayChar14() {
		return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
	}

	/**
	 * 返回 年月日小时分秒 毫秒
	 * 
	 * @return yyyyMMddHHmmssS
	 */
	public static String getTodayChar17() {
		return DateFormatUtils.format(new Date(), "yyyyMMddHHmmssS");
	}

	/**
	 * 返回中文日期格式 支持4、6、8、12、14、17位转换
	 * 
	 * @param charDateTime
	 *            长整型 CHAR
	 * @return 2007年12月12日 12时12分12秒 234毫秒
	 */
	public static String getFormatChina(String charDateTime) {
		return getFormatDateTime(charDateTime, "FORMAT_CHINA");
	}

	/**
	 * 返回标准日期格式 支持4、6、8、12、14、17位转换
	 * 
	 * @param charDateTime
	 *            长整型 CHAR
	 * @return 2007-12-12 12:12:12 234
	 */
	public static String getFormatNormal(String charDateTime) {
		return getFormatDateTime(charDateTime, "FORMAT_NORMAL");
	}

	/**
	 * 返回标准日期格式
	 * 
	 * @author fxcai 2013-6-12 上午11:42:06
	 * @param dateTime
	 * @return 2007-12-12 12:12:12 234
	 */
	public static String getFormatNormal(Date dateTime) {
		return DateFormatUtils.format(dateTime, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 返回标准日期格式 支持4、6、8、12、14、17位转换
	 * 
	 * @param charDateTime
	 *            长整型 CHAR
	 * @return 2007/12/12 12:12:12 234
	 */
	public static String getFormatDateTime(String charDateTime) {
		return getFormatDateTime(charDateTime, "FORMAT_DATATIME");
	}

	/**
	 * 把日期格式转换为长整型格式
	 * 
	 * @param inputDateTime
	 * @return
	 */
	public static String getDateTimeToChar(String inputDateTime) {
		String strResult = "";
		if (null == inputDateTime) {
			return strResult = "";
		}

		if (("".equals(inputDateTime.trim()))) {
			return strResult = "";
		}

		// 替换
		strResult = inputDateTime.replaceAll("年", "");
		strResult = strResult.replaceAll("月", "");
		strResult = strResult.replaceAll("日", "");
		strResult = strResult.replaceAll("时", "");
		strResult = strResult.replaceAll("分", "");
		strResult = strResult.replaceAll("秒", "");
		strResult = strResult.replaceAll("毫", "");
		strResult = strResult.replaceAll(" ", "");
		strResult = strResult.replaceAll("-", "");
		strResult = strResult.replaceAll("/", "");
		strResult = strResult.replaceAll(":", "");
		return strResult;

	}

	/**
	 * 对日期进行转换
	 * 
	 * @param charDateTime
	 * @param formatType
	 * @return
	 */
	private static String getFormatDateTime(String charDateTime,
			String formatType) {
		String strResult = "";
		if (null == charDateTime) {
			return strResult = "";
		}

		if (("".equals(charDateTime.trim()))) {
			return strResult = "";
		}

		String[] FORMAT_CHAR = null;
		if (formatType.equals("FORMAT_CHINA")) {
			FORMAT_CHAR = FORMAT_CHINA;
		} else if (formatType.equals("FORMAT_NORMAL")) {
			FORMAT_CHAR = FORMAT_NORMAL;
		} else if (formatType.equals("FORMAT_DATATIME")) {
			FORMAT_CHAR = FORMAT_DATATIME;
		} else {
			return strResult = charDateTime;
		}

		// 去掉空格
		charDateTime = charDateTime.trim();

		if (charDateTime.length() == 4) {
			// MMdd 转换 MM月dd日
			strResult = charDateTime.substring(0, 2) + FORMAT_CHAR[1]
					+ charDateTime.substring(2, 4) + FORMAT_CHAR[2];
		} else if (charDateTime.length() == 6) {
			// yyyyMM 转换 yyyy年MM月
			strResult = charDateTime.substring(0, 4) + FORMAT_CHAR[0]
					+ charDateTime.substring(4, 6) + FORMAT_CHAR[1];
		} else if (charDateTime.length() == 8) {
			// yyyyMMdd
			strResult = charDateTime.substring(0, 4) + FORMAT_CHAR[0]
					+ charDateTime.substring(4, 6) + FORMAT_CHAR[1]
					+ charDateTime.substring(6, 8) + FORMAT_CHAR[2];
		} else if (charDateTime.length() == 12) {
			// yyyyMMddHHmm
			strResult = charDateTime.substring(0, 4) + FORMAT_CHAR[0]
					+ charDateTime.substring(4, 6) + FORMAT_CHAR[1]
					+ charDateTime.substring(6, 8) + FORMAT_CHAR[2] + " "
					+ charDateTime.substring(8, 10) + FORMAT_CHAR[3]
					+ charDateTime.substring(10, 12) + FORMAT_CHAR[4];
		} else if (charDateTime.length() == 14) {
			// yyyyMMddHHmmss
			strResult = charDateTime.substring(0, 4) + FORMAT_CHAR[0]
					+ charDateTime.substring(4, 6) + FORMAT_CHAR[1]
					+ charDateTime.substring(6, 8) + FORMAT_CHAR[2] + " "
					+ charDateTime.substring(8, 10) + FORMAT_CHAR[3]
					+ charDateTime.substring(10, 12) + FORMAT_CHAR[4]
					+ charDateTime.substring(12, 14) + FORMAT_CHAR[5];
		} else if (charDateTime.length() == 17) {
			// yyyyMMddHHmmssS
			strResult = charDateTime.substring(0, 4) + FORMAT_CHAR[0]
					+ charDateTime.substring(4, 6) + FORMAT_CHAR[1]
					+ charDateTime.substring(6, 8) + FORMAT_CHAR[2] + " "
					+ charDateTime.substring(8, 10) + FORMAT_CHAR[3]
					+ charDateTime.substring(10, 12) + FORMAT_CHAR[4]
					+ charDateTime.substring(12, 14) + FORMAT_CHAR[5] + " "
					+ charDateTime.substring(14, 17) + FORMAT_CHAR[6];
		} else {
			strResult = charDateTime;
		}

		return strResult;
	}

	/**
	 * 将指定Date类型参数转换为指定的Oracle日期时间格式字符串
	 * 
	 * @param inputDate
	 *            传入Date类型参数
	 * @return String
	 */
	public static String getOracleDate(Date inputDateTime)
			throws NullPointerException {
		if (null == inputDateTime) {
			return "";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		return dateFormat.format(inputDateTime);
	}

	/**
	 * 比对两个时间间隔
	 * 
	 * @param startDateTime
	 *            开始时间
	 * @param endDateTime
	 *            结束时间
	 * @param distanceType
	 *            计算间隔类型 天d 小时h 分钟m 秒s
	 * @return
	 */
	public static String getDistanceDT(String startDateTime,
			String endDateTime, String distanceType) {
		String strResult = "";
		long lngDistancVal = 0;
		try {
			SimpleDateFormat tempDateFormat = new SimpleDateFormat(
					"yyyyMMddHHmmss");
			Date startDate = tempDateFormat.parse(startDateTime);
			Date endDate = tempDateFormat.parse(endDateTime);
			if (distanceType.equals("d")) {
				lngDistancVal = (endDate.getTime() - startDate.getTime())
						/ (1000 * 60 * 60 * 24);
			} else if (distanceType.equals("h")) {
				lngDistancVal = (endDate.getTime() - startDate.getTime())
						/ (1000 * 60 * 60);
			} else if (distanceType.equals("m")) {
				lngDistancVal = (endDate.getTime() - startDate.getTime())
						/ (1000 * 60);
			} else if (distanceType.equals("s")) {
				lngDistancVal = (endDate.getTime() - startDate.getTime()) / (1000);
			}
			strResult = String.valueOf(lngDistancVal);
		} catch (Exception e) {
			strResult = "0";
		}
		return strResult;
	}

	/**
	 * 得到日期毫秒数
	 * 
	 * @author lixin 2011-10-21 下午03:03:32
	 * @param strDate
	 * @return
	 */
	public static long currentTimeMillis(String strDate) {
		long timeMillis = 0;
		if (strDate == null || strDate.trim().length() != 10) {
			return timeMillis;
		}
		String year = strDate.trim().substring(0, 4);
		String month = strDate.trim().substring(5, 7);
		String day = strDate.trim().substring(8, 10);
		timeMillis = Long.parseLong(year + month + day + "000000");
		return timeMillis;
	}

	public static long currentDateMillis(String strDate) {
		long timeMillis = 0;
		try {
			String tem = dateFormat.format(dateFormat_3.parse(strDate));
			timeMillis = Long.parseLong(tem);
		} catch (ParseException e) {
		}
		return timeMillis;
	}

	public static long currentWeekMillis(int year, int month) {
		try {
			Calendar c = Calendar.getInstance();
			c.set(year, month - 1, 1);
			int tem = c.get(Calendar.DAY_OF_WEEK);
			return currentDateMillis(year + "-" + month + "-" + (10 - tem));
		} catch (Exception e) {
			return 0;
		}
	}

	public static long currentLastDayOfMonthMillis(int year, int month) {
		try {
			Calendar c = Calendar.getInstance();
			c.set(Calendar.YEAR, year);
			c.set(Calendar.MONTH, month - 1);
			int tem = c.getActualMaximum(Calendar.DAY_OF_MONTH);
			return currentDateMillis(year + "-" + month + "-" + tem);
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 得到日期小时数
	 * 
	 * @author lixin 2011-10-21 下午03:06:14
	 * @param date
	 * @return
	 */
	public static String getHour(String date) {
		try {
			return dateFormat_4.format(dateFormat.parse(date));
		} catch (ParseException e) {
			return "";
		}
	}

	/**
	 * 得到日期天数
	 * 
	 * @author lixin 2011-10-21 下午03:05:55
	 * @param date
	 * @return
	 */
	public static String getDay(String date) {
		try {
			return dateFormat_5.format(dateFormat.parse(date));
		} catch (ParseException e) {
			return "";
		}
	}

	public static String formatDateToOther(String date) {
		try {
			return dateFormat_3.format(dateFormat.parse(date));
		} catch (ParseException e) {
			return "";
		}
	}

	/**
	 * 得到日期字符串
	 * 
	 * @author lixin 2011-10-21 下午03:06:40
	 * @param date
	 * @return
	 */
	public static String getDate(String date) {
		String strResult = "";
		if (date.length() >= 8) {
			strResult += date.substring(0, 4) + "-";
			strResult += date.substring(4, 6) + "-";
			strResult += date.substring(6, 8);
		}

		return strResult;
	}

	/**
	 * 得到日期小时字符串
	 * 
	 * @author lixin 2011-10-21 下午03:07:01
	 * @param date
	 * @return
	 */
	public static String getDateHour(String date) {
		String strResult = "";
		if (date.length() >= 10) {
			strResult += date.substring(0, 4) + "-";
			strResult += date.substring(4, 6) + "-";
			strResult += date.substring(6, 8) + " ";
			strResult += date.substring(date.length() - 2);
		}

		return strResult;
	}

	public static String getDateChar8(String date) {
		String strResult = "";
		if (date.length() >= 10) {
			strResult += date.substring(0, 4);
			strResult += date.substring(5, 7);
			strResult += date.substring(8);
		}

		return strResult;
	}

	public static boolean compareDates(String date1, String date2, String format)
			throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat(format);
		try {
			Date d1 = sf.parse(date1);
			Date d2 = sf.parse(date2);
			return d1.after(d2);
		} catch (ParseException e) {
			throw e;
		}
	}

	// 常量------------------------------------------------------------------
	private static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyyMMddHHmmss");
	private static SimpleDateFormat dateFormatSSS = new SimpleDateFormat(
	"yyyyMMddHHmmss.SSS");
	private static SimpleDateFormat dateFormat_s = new SimpleDateFormat(
	"yyyy-MM-dd HH:mm:ss");
	
	private static SimpleDateFormat dateFormat_2 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat dateFormat_3 = new SimpleDateFormat(
			"yyyy-MM-dd");
	private static SimpleDateFormat dateFormat_4 = new SimpleDateFormat(
			"HH:mm:ss");
	private static SimpleDateFormat dateFormat_5 = new SimpleDateFormat("dd/MM");
	private static SimpleDateFormat dateFormat_6 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");

	public static String getFileTime(String time) {
		String reTime = "";
		try {
			reTime = dateFormat_s.format(dateFormatSSS.parse(time));
		} catch (ParseException e) {
		}
		return reTime;
	}
	public static String formatDateStrToOtherStr(String time) {
		String reTime = "";
		try {
			reTime = dateFormat_2.format(dateFormat.parse(time));
		} catch (ParseException e) {
		}
		return reTime;
	}

	public static long formatDate(String strDate) {
		long reDate = 0;
		try {
			Date date = dateFormat.parse(strDate);
			String tem = dateFormat.format(date);
			reDate = Long.parseLong(tem);
		} catch (ParseException e) {
		}
		return reDate;
	}

	/**
	 * 日期差计算 例如在某个日期增加几天后的日期 返回几天后日期
	 * 
	 * @param startDate
	 * @param addDate
	 * @return
	 */
	public static String getIncreaseDT(String startDate, int addDate) {
		String strResult = "0000-00-00";

		try {
			// 把字符串型日期转换为日期
			Calendar localDate = new GregorianCalendar();
			SimpleDateFormat tempDateFormat = new SimpleDateFormat("yyyyMMdd");
			Date tempDate = tempDateFormat.parse(startDate);
			localDate.setTime(tempDate);
			localDate.add(Calendar.DATE, addDate);

			String curyear = "" + localDate.get(Calendar.YEAR);
			int intmonth = localDate.get(Calendar.MONTH) + 1;
			String curmonth = "" + intmonth;
			String curday = "" + localDate.get(Calendar.DAY_OF_MONTH);

			if (curmonth.length() == 1) {
				curmonth = "0" + curmonth;
			}
			if (curday.length() == 1) {
				curday = "0" + curday;
			}
			strResult = curyear + "" + curmonth + "" + curday;
		} catch (Exception e) {
		}
		return strResult;
	}

	/**
	 * 获取本周的开始日期 （星期天的日期）20070201
	 * 
	 * @return
	 */
	public static String getWeekStartDate() {
		String strResult = "19000101";
		try {
			Calendar calendar = Calendar.getInstance();
			int intWeekNum = calendar.get(Calendar.DAY_OF_WEEK);
			intWeekNum = intWeekNum - 1;
			strResult = getIncreaseDT(getTodayChar8(), -intWeekNum);
		} catch (Exception ex) {
			strResult = "19000101";
		}
		return strResult;
	}

	/**
	 * 获取今天星期几 中文
	 * 
	 * @return
	 */
	public static String getWeekChina() {
		String strResult = " ";
		try {
			Calendar calendar = Calendar.getInstance();
			int intWeekNum = calendar.get(Calendar.DAY_OF_WEEK);
			intWeekNum = intWeekNum - 1;
			strResult = FORMAT_WEEK[intWeekNum];
		} catch (Exception ex) {
			strResult = " ";
		}
		return strResult;
	}

	/**
	 * 获取昨天的日期
	 * 
	 * @return
	 */
	public static String getYestodayDate() {

		Calendar cal = Calendar.getInstance();

		cal.add(Calendar.DATE, -1);

		String yesterday = new SimpleDateFormat("yyyyMMdd").format(cal
				.getTime());

		return yesterday;
	}

	/**
	 * util.Date型日期转化指定格式的字符串型日期
	 * 
	 * @param p_date
	 *            Date
	 * @param p_format
	 *            String 格式1:"yyyy-MM-dd" 格式2:"yyyy-MM-dd hh:mm:ss EE"
	 *            格式3:"yyyy年MM月dd日 hh:mm:ss EE" 说明: 年-月-日 时:分:秒 星期 注意MM/mm大小写
	 * @return String
	 */
	public static String toStrDateFromUtilDateByFormat(
			java.util.Date p_utilDate, String p_format) throws ParseException {
		String l_result = "";
		if (p_utilDate != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(p_format);
			l_result = sdf.format(p_utilDate);
		}
		return l_result;
	}

	/**
	 * 字符串转日期
	 * 
	 * @author lixin 2012-1-18 下午02:17:50
	 * @param date
	 * @return
	 */
	public static Date stringToDate(String dateStr) {
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 计算时间长度：s
	 * 
	 * @author ls 2013-6-25 上午10:17:39
	 * @param time
	 *            "hh:mm:ss"
	 * @return
	 */
	public static int calculateSecond(String time) {
		int allTime = 0;
		String[] split = time.split(":");
		if (split != null) {
			int length = split.length;
			if (length == 1) {
				allTime = Integer.parseInt(split[0]);
			} else {
				for (int i = 0; i < length; ++i) {
					int t = Integer.parseInt(split[i]);
					if (i == 0) {
						allTime += t * 60 * 60;
					} else if (i == 1) {
						allTime += t * 60;
					} else if (i == 2) {
						allTime += t;
					}
				}
			}
		}
		return allTime;
	}

	/**
	 * 将时间字符串转化为Timestamp
	 * 
	 * @author ls 2013-6-25 上午10:32:40
	 * @param dateStr
	 *            yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static Timestamp stringToTimestamp(String dateStr) {
		if (dateStr == null || "".equals(dateStr.trim())) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Timestamp date = null;
		try {
			date = new Timestamp(sdf.parse(dateStr).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 
	 * @author fxcai 2013-6-25 下午05:28:27
	 * @param dateStr
	 *            yyyy-MM-dd HH:mm
	 * @return
	 */
	public static Timestamp stringToShortTimestamp(String dateStr) {
		if (dateStr == null || "".equals(dateStr.trim())) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Timestamp date = null;
		try {
			date = new Timestamp(sdf.parse(dateStr).getTime());
		} catch (ParseException e) {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
			try {
				date = new Timestamp(sdf1.parse(dateStr).getTime());
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
		}
		return date;
	}

	public static Date stringToDates(String dateStr) {
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	
	/**
	 * 获取当前时间
	 * @author ls 2013-11-4 下午04:26:15
	 * @return
	 */
	public static Timestamp getCurrentTimestamp() {
		Timestamp olddate = new java.sql.Timestamp(new Date().getTime());
		return olddate;
	}
	
	/**
	 * 获取当前时间 
	 * yyyy-MM-dd HH:mm:ss
	 * @author ls 2013-11-4 下午04:26:03
	 * @return
	 */
	public static String getCurrentTimestr() {
		Timestamp olddate = new java.sql.Timestamp(new Date().getTime());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = olddate.toString();
		try {
			olddate = new Timestamp(sdf.parse(olddate.toString()).getTime());
			String str = date.toString();
			int index = str.indexOf(".");
			if(index>0){
				str = str.substring(0, index);
			}
			date = str;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	public static Timestamp getLongTimeByshort(String dateStr) {
		if (dateStr == null || "".equals(dateStr.trim())) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Timestamp date = null;
		try {
			date = new Timestamp(sdf.parse(dateStr).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Object getFormatMinDate(Object time) {
		String min = " 00:00:00";
		try {
			if (time instanceof String) {
				return dateFormat_3.format(dateFormat_2.parse(time.toString()))
						+ min;
			} else if (time instanceof Timestamp) {
				String reTime = dateFormat_3.format(dateFormat_2.parse(time
						.toString()))
						+ min;
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				Timestamp date = null;
				try {
					date = new Timestamp(sdf.parse(reTime).getTime());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				return date;
			} else if (time instanceof Date) {
				String reTime = dateFormat_3.format(time) + min;
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				// Date date=null;
				// try {
				// date = sdf.parse(reTime);
				// } catch (ParseException e) {
				// e.printStackTrace();
				// }
				Timestamp date = null;
				try {
					date = new Timestamp(sdf.parse(reTime).getTime());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				return date;
			}
		} catch (ParseException e) {
		}
		return time;
	}

	public static Object getFormatMaxDate(Object time) {
		String max = " 23:59:59";
		try {
			if (time instanceof String) {
				return dateFormat_3.format(dateFormat_2.parse(time.toString()))
						+ max;
			} else if (time instanceof Timestamp) {
				String reTime = dateFormat_3.format(dateFormat_2.parse(time
						.toString()))
						+ max;
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				Timestamp date = null;
				try {
					date = new Timestamp(sdf.parse(reTime).getTime());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				return date;
			} else if (time instanceof Date) {
				String reTime = dateFormat_3.format(time) + max;
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				// Date date=null;
				// try {
				// date = sdf.parse(reTime);
				// } catch (ParseException e) {
				// e.printStackTrace();
				// }
				Timestamp date = null;
				try {
					date = new Timestamp(sdf.parse(reTime).getTime());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				return date;
			}
		} catch (ParseException e) {
		}
		return time;
	}

	public static String getFormatDate(long time) {
		if (time <= 0) {
			return "";
		}
		Calendar c1 = Calendar.getInstance();
		c1.setTime(new Date(time));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			String date = sdf.format(c1.getTime());
			if (date != null) {
				return date.toString();
			}
		} catch (Exception e) {
			try {
				String date = format.format(c1.getTime());
				if (date != null) {
					return date.toString();
				}
			} catch (Exception e1) {
			}
		}
		return "";
	}

	public static String parseEnglishDate(String data) {
		try{
			if (data != null && data.length() == 28 && data.indexOf("CST") == 20) {
				String []str = data.split("\\s+");
				
				String year = str[5];
				String date = str[2];
				String time = str[3];
				String temp = str[1];
				if (temp.equals("Jan")) {
					data = year + "-" + "01" + "-" + date;
				}
				if (temp.equals("Feb")) {
					data = year + "-" + "02" + "-" + date;
				}
				if (temp.equals("Mar")) {
					data = year + "-" + "03" + "-" + date;
				}
				if (temp.equals("Apr")) {
					data = year + "-" + "04" + "-" + date;
				}
				if (temp.equals("May")) {
					data = year + "-" + "05" + "-" + date;
				}
				if (temp.equals("Jun")) {
					data = year + "-" + "06" + "-" + date;
				}
				if (temp.equals("Jul")) {
					data = year + "-" + "07" + "-" + date;
				}
				if (temp.equals("Aug")) {
					data = year + "-" + "08" + "-" + date;
				}
				if (temp.equals("Sep")) {
					data = year + "-" + "09" + "-" + date;
				}
				if (temp.equals("Oct")) {
					data = year + "-" + "10" + "-" + date;
				}
				if (temp.equals("Nov")) {
					data = year + "-" + "11" + "-" + date;
				}
				if (temp.equals("Dec")) {
					data = year + "-" + "12" + "-" + date;
				}
				return data+" "+time;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
	
	/**
	 * date转成字符串
	 * @author zhanghong 2012-11-1 上午09:02:44
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date,String formatStr){
		String mDate="";
		
		SimpleDateFormat format=new SimpleDateFormat(formatStr);
		try {
			mDate = format.format(date);
        } catch (Exception e) {
	        e.printStackTrace();
        }
		return mDate;
	}

	/**
	 * 
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
		// System.out.println(DateTimeUtil.getWeekChina());
		// System.out.println(DateTimeUtil.getDateTimeToChar("2007-02-02 12:12:12 234"));
		// System.out.println(getDateChar8("2009-06-08"));
		// try {
		// System.out.println(compareDates("2009061108", "2009061108",
		// "yyyyMMddHH"));
		// } catch (ParseException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }s
		Calendar c1 = Calendar.getInstance();
		c1.setTime(new Date());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ENGLISH);
		 System.out.println(  parseEnglishDate("Fri Sep 13 17:59:47 CST 2013"));
		//System.out.println(getFormatDate(new Date().getTime()));
	}
}
